import java.util.Arrays;

public class Test {

    public static int BinarySearch(int[] array, int key) {
        int left = 0;
        int right = array.length - 1;
        int mid = (left + right) / 2;
        while (array[left] <= array[right]) {
            if (array[mid] < key) {
                left = mid + 1;
            }else if (array[mid] > key) {
                right = mid - 1;
            } else {
                return mid;
            }
            mid = (left + right) / 2;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] array = {12,20,45,56,67};
        int key = 56;
        int ret = BinarySearch(array,key);
        if (ret == -1) {
            System.out.println("没找到这个数");
        } else{
            System.out.println("找到了，下标是" + ret);
        }
    }

    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] > array[j]){
                    swap(array,i,j);
                }
            }
        }
        System.out.println(Arrays.toString(array));
    }

    public static void main6(String[] args) {
        int[] array = {1,2,34,3,4,5,7,23,12};
        bubbleSort(array);
    }

    public static void func(int[] array) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 != 0) {
                count++;
            }else{
                count = 0;
            }
            if (count == 3) {
                System.out.println("存在连续三个奇数");
                return;
            }
        }
    }
    public static void main5(String[] args) {
        int[] array = {1,2,34,3,4,5,7,23,12};
        func(array);
    }


    public static void main4(String[] args) {
        int[] array = {2,2,1,1,1,2,2};
        Arrays.sort(array);
        int result = array[array.length / 2];
        System.out.println(result);
    }

    public static int findOnlyNum(int[] array) {
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }
    public static void main3(String[] args) {
        int[] array = {4,1,2,1,2};
        int ret = findOnlyNum(array);
        System.out.println(ret);
    }
    public static int[] addArrayNum(int[] array, int target) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 1; j < array.length; j++) {
                if (array[i] + array[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }

    public static void main2(String[] args) {
        int[] array = {2,7,11,15};
        int target = 9;
        int[] ret = addArrayNum(array,target);
        System.out.println(Arrays.toString(ret));
    }

    public static void changeNum(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            while ((left < right) && (array[left] % 2 != 0)) {
                left++;
            }
            while ((left < right) && (array[right] % 2 == 0)) {
                right--;
            }
            swap(array, left, right);
        }
    }

    /*public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }*/

    public static void main1(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6};
        changeNum(array);
        System.out.println(Arrays.toString(array));
    }
}